<template>
  <div class="admin-login-container">
    <div class="login-background">
      <div class="background-overlay"></div>
    </div>
    
    <div class="login-card">
      <div class="card-header">
        <div class="logo-section">
          <div class="admin-icon">
            <i class="el-icon-user-solid"></i>
          </div>
          <h1 class="title">管理员登录</h1>
          <p class="subtitle">Admin Login Portal</p>
        </div>
      </div>
      
      <div class="card-body">
        <div class="form-group">
          <label class="form-label">
            <i class="el-icon-user"></i>
            管理员账号
          </label>
          <input 
            type="text" 
            v-model="form.username" 
            placeholder="请输入管理员账号"
            class="form-input"
            @keydown.enter="login"
          >
        </div>
        
        <div class="form-group">
          <label class="form-label">
            <i class="el-icon-lock"></i>
            管理员密码
          </label>
          <input 
            type="password" 
            v-model="form.password" 
            placeholder="请输入管理员密码"
            class="form-input"
            @keydown.enter="login"
          >
        </div>
        
        <div class="button-group">
          <button @click="goBack()" class="btn btn-secondary">
            <i class="el-icon-back"></i>
            返回
          </button>
          <button @click="login()" class="btn btn-primary">
            <i class="el-icon-right"></i>
            登录
          </button>
        </div>
        
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "LoginView",
  created() {
    document.title = '管理员登录';
  },
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      loading: false
    }
  },
  methods: {
    login() {
      // 表单验证
      if (!this.form.username) {
        this.$message.error('请输入管理员账号');
        return;
      }
      if (!this.form.password) {
        this.$message.error('请输入管理员密码');
        return;
      }
      
      this.loading = true;
      
      request.post('/user/manageLogin', this.form).then(res => {
        if (res.code === '0') {
          this.$message({
            type: 'success',
            message: "管理员登录成功",
            duration: 2000
          });
          // 缓存用户信息
          sessionStorage.setItem("user", JSON.stringify(res.data));
          // 跳转主页，检测用户的角色是否为管理员，如果是admin才跳转管理页面
          setTimeout(() => {
            this.$router.push('/charts');
          }, 1000);
        } else {
          this.$message({
            type: 'error',
            message: res.msg,
            duration: 3000
          })
        }
      }).catch(error => {
        this.$message.error('登录失败，请检查网络连接');
      }).finally(() => {
        this.loading = false;
      })
    },
    goBack() {
      window.history.back();
    }
  }
}
</script>
<style scoped>
/* 全局容器 */
.admin-login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

/* 背景设计 */
.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url("@/assets/images/login.jpg") no-repeat;
  background-size: cover;
  z-index: 1;
}

.background-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
}

/* 登录卡片 */
.login-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  box-shadow: 
    0 20px 40px rgba(0, 0, 0, 0.1),
    0 0 0 1px rgba(255, 255, 255, 0.2);
  width: 420px;
  max-width: 90vw;
  position: relative;
  z-index: 2;
  overflow: hidden;
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 卡片头部 */
.card-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px 30px 30px;
  text-align: center;
  position: relative;
}

.card-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="white" opacity="0.1"/><circle cx="75" cy="75" r="1" fill="white" opacity="0.1"/><circle cx="50" cy="10" r="0.5" fill="white" opacity="0.1"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  opacity: 0.3;
}

.logo-section {
  position: relative;
  z-index: 1;
}

.admin-icon {
  width: 80px;
  height: 80px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px);
}

.admin-icon i {
  font-size: 36px;
  color: white;
}

.title {
  color: white;
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 8px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin: 0;
  font-weight: 300;
  letter-spacing: 1px;
}

/* 卡片主体 */
.card-body {
  padding: 40px 30px;
}

/* 表单组 */
.form-group {
  margin-bottom: 24px;
}

.form-label {
  display: flex;
  align-items: center;
  color: #4a5568;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
}

.form-label i {
  margin-right: 8px;
  color: #667eea;
  font-size: 16px;
}

.form-input {
  width: 100%;
  padding: 16px 20px;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: #f8fafc;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  transform: translateY(-1px);
}

.form-input::placeholder {
  color: #a0aec0;
}

/* 按钮组 */
.button-group {
  display: flex;
  gap: 12px;
  margin: 32px 0;
}

.btn {
  flex: 1;
  padding: 16px 24px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  overflow: hidden;
}

.btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.btn:hover::before {
  left: 100%;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.6);
}

.btn-secondary {
  background: #f7fafc;
  color: #4a5568;
  border: 2px solid #e2e8f0;
}

.btn-secondary:hover {
  background: #edf2f7;
  border-color: #cbd5e0;
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}


/* 响应式设计 */
@media (max-width: 480px) {
  .login-card {
    width: 95vw;
    margin: 20px;
  }
  
  .card-header {
    padding: 30px 20px 20px;
  }
  
  .card-body {
    padding: 30px 20px;
  }
  
  .title {
    font-size: 24px;
  }
  
  .button-group {
    flex-direction: column;
  }
}

/* 加载状态 */
.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}

/* 动画效果 */
.form-group {
  animation: fadeInUp 0.6s ease-out;
  animation-fill-mode: both;
}

.form-group:nth-child(1) { animation-delay: 0.1s; }
.form-group:nth-child(2) { animation-delay: 0.2s; }
.button-group { animation: fadeInUp 0.6s ease-out 0.3s both; }

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
